package com.ia2014.tp.portal2.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ia2014.tp.portal2.facades.FacadeVenta;
import com.ia2014.tp.portal2.vo.ItemVentaVO;

@WebServlet("/acutalizarCarrito")
public class ActualizarCarritoServlet extends AbstractServlet{
	private static final long serialVersionUID = 6454865243645207085L;
	
	@EJB
	private FacadeVenta facade;
	
	
	public ActualizarCarritoServlet(){
		super();
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.comprobarSession(req, resp);
		this.process(req, resp);
	}

	
	private void process(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException{
		//Levanto la session y saco los parametros de configuracion
		HttpSession session  = req.getSession(false);
		Integer idArticulo 	 = Integer.parseInt(req.getParameter("idArticulo"));
		Integer cantidadItem = Integer.parseInt(req.getParameter("cantidadItem"));
		String redirect = "verCarrito";
		
		this.getMapItemsFromSession(session);
		ItemVentaVO itemVo = this.itemsVo.get(idArticulo);
		if (itemVo != null){
			itemVo.setCantidad(cantidadItem);
			this.itemsVo.put(idArticulo, itemVo);
			this.facade.setUsuarioVo(this.getUsuarioVOFromSession(session));
			this.facade.setItemsVo(this.itemsVo);
			this.facade.actualizarCantidadItem(idArticulo, cantidadItem);
		}

		resp.sendRedirect(redirect);
	}	
}
